home *** CD-ROM | disk | FTP | other *** search
/ Super PC 34 / Super PC 34 (Shareware).iso / spc / UTIL / DJGPP2 / CONTRIB / ZLIB_PC.ZIP / zlib_pc / zfopen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-20  |  2.5 KB  |  133 lines

  1. #include "zdef.h"
  2.  
  3. /*------------------------------*/
  4. /*    zfopen            */
  5. /*------------------------------*/
  6. #ifndef __STDC__
  7. ZFILE *zfopen (fileptr, how)
  8. char   *fileptr;
  9. char   *how;
  10. #else
  11. ZFILE *zfopen (char *fileptr, char *how)
  12. #endif
  13. {
  14.     ZFILE *z;
  15.  
  16.  
  17.  
  18.     /*
  19.      *   allocate space for the z structure
  20.      */
  21.     z = (ZFILE *) malloc (sizeof (ZFILE));
  22.     if (z == (ZFILE *) NULL)
  23.     {
  24.         return ((ZFILE *) z);
  25.     }
  26.     
  27.  
  28.     /*
  29.      *   set up the structure...
  30.      */
  31.     z->flags          = 0;
  32.     z->maxbits        = Z_BITS;    /* user settable max # bits/code */
  33.     z->free_ent       = 0;        /* first unused entry */
  34.     z->block_compress = BLOCK_MASK;
  35.     z->clear_flg      = 0;
  36.     z->init           = 0;
  37.  
  38.     z->zeof           = (0 != 0);
  39.     z->c1             = EOF;
  40.     z->c2             = EOF;
  41.     z->bufput         = 0;
  42.     z->bufget         = 0;
  43.     z->bufend         = Z_MAXBUF - 1;
  44.  
  45.     z->maxbits        = Z_BITS;    /* user settable max # bits/code */
  46.  
  47.  
  48.  
  49.     /*
  50.      *   Open input file
  51.      */
  52.     if (*how == 'r')
  53.     {
  54. #if defined(ALCYON)
  55.         z->file = fopenb (fileptr, "r");
  56. #elif defined(MSDOS) || defined(atarist)
  57.         z->file = fopen (fileptr, "rb");
  58. #else
  59.         z->file = fopen (fileptr, "r");
  60. #endif
  61.         if (z->file == (FILE *) NULL)
  62.         {
  63.             char    tempfname[256];
  64.  
  65.             strcpy (tempfname, fileptr);
  66.             strcat (tempfname, ZEXT);
  67. #if defined(ALCYON)
  68.             z->file = fopenb (tempfname, "r");
  69. #elif defined(MSDOS) || defined(atarist)
  70.             z->file = fopen (tempfname, "rb");
  71. #else
  72.             z->file = fopen (tempfname, "r");
  73. #endif
  74.         }
  75.     }
  76.     else
  77.     {
  78.         /*
  79.          *   No compressed output yet, if ever...
  80.          *   Compress the file explicitly once it has been written
  81.          */
  82. #if defined(ALCYON)
  83.         z->file = fopenb (fileptr, how);
  84. #else
  85.         z->file = fopen (fileptr, how);
  86. #endif
  87.         z->flags |= NOT_COMPRESSED;
  88.     }
  89.     if (z->file == (FILE *) NULL)
  90.     {
  91.         free (z);
  92.         z = (ZFILE *) NULL;
  93.     }
  94.  
  95.     /*
  96.      *   Check the magic number
  97.      */
  98.     if ((z != (ZFILE *) NULL)
  99.     && ((fgetc (z->file) != 0x1F) || (fgetc (z->file) != 0x9D)))
  100.     {
  101.         z->flags |= NOT_COMPRESSED;
  102.         fclose (z->file);
  103. #ifdef ALCYON
  104.         z->file = fopenb (fileptr, how);
  105. #else
  106.         z->file = fopen (fileptr, how);
  107. #endif
  108.         if (z->file == (FILE *) NULL)
  109.         {
  110.             free (z);
  111.             z = (ZFILE *) NULL;
  112.         }
  113.     }
  114.     if ((z == (ZFILE *) NULL) || ((z->flags & NOT_COMPRESSED) != 0))
  115.         return ((ZFILE *) z);
  116.  
  117.     z->maxbits        = fgetc (z->file);    /* set -b from file */
  118.     z->block_compress = z->maxbits & BLOCK_MASK;
  119.     z->maxbits       &= BIT_MASK;
  120.  
  121.     if (z->maxbits > Z_BITS)
  122.     {
  123.         fprintf (stderr,
  124.     "%s: compressed with %d bits; decompress can only handle %d bits\n",
  125.             fileptr, z->maxbits, Z_BITS);
  126.         exit (0);
  127.     }
  128.  
  129.     return ((ZFILE *) z);
  130. }
  131.  
  132.  
  133.